package com.sq.partner.manager.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

import javax.servlet.http.HttpServletRequest;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.apache.poi.xwpf.converter.core.FileImageExtractor;
import org.apache.poi.xwpf.converter.core.FileURIResolver;
import org.apache.poi.xwpf.converter.xhtml.XHTMLConverter;
import org.apache.poi.xwpf.converter.xhtml.XHTMLOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.http.HttpRequest;
import org.w3c.dom.Document;


public class DocToHtml {
	
	
	private static org.apache.log4j.Logger logger = LogUtil.getLog(DocToHtml.class);
		
	
	/**
	 * 转换docx
	 * @param filePath
	 * @param fileName
	 * @param htmlName
	 * @throws Exception
	 */
	public static String docx(InputStream in,String htmlName) throws Exception{
		
		//直接参数为inputStream;
		XWPFDocument document = new XWPFDocument(in);
		// ) 解析 XHTML配置 (这里设置IURIResolver来设置图片存放的目录)
		File imageFolderFile = new File("C:\\");
		XHTMLOptions options = XHTMLOptions.create().URIResolver(new FileURIResolver(imageFolderFile));
		options.setExtractor(new FileImageExtractor(imageFolderFile));
		options.setIgnoreStylesIfUnused(false);
		options.setFragment(true);
		// ) 将 XWPFDocument转换成XHTML
		File file = new File("text.txt");
		OutputStream out = new FileOutputStream(file);
		
		XHTMLConverter.getInstance().convert(document, out, options);
		
		InputStream is = new FileInputStream(file);
		byte[] bts = new byte[0];
		bts = new byte[is.available()];
		is.read(bts);
		String content = new String(bts); 
		logger.debug(content);
		return content;
				
	}
	/**
	 * 转换doc
	 * @param filePath
	 * @param fileName
	 * @param htmlName
	 * @throws Exception
	 */
	public static void dox(String filePath ,String fileName,String htmlName) throws Exception{
        final String file = filePath + fileName;
        InputStream input = new FileInputStream(new File(file));
        HWPFDocument wordDocument = new HWPFDocument(input);	
        WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
       //解析word文档
       wordToHtmlConverter.processDocument(wordDocument);
       Document htmlDocument = wordToHtmlConverter.getDocument();
       
       File htmlFile = new File(filePath + htmlName);
       OutputStream outStream = new FileOutputStream(htmlFile);
       
       DOMSource domSource = new DOMSource(htmlDocument);
       StreamResult streamResult = new StreamResult(outStream);
 
       TransformerFactory factory = TransformerFactory.newInstance();
       Transformer serializer = factory.newTransformer();
       serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
       serializer.setOutputProperty(OutputKeys.INDENT, "yes");
       serializer.setOutputProperty(OutputKeys.METHOD, "html");
       
       serializer.transform(domSource, streamResult);
       outStream.close();
   }
}
